package roleView 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	/**
	 * @author Kylin206
	 */
	public class BmpSprite extends Sprite implements IMovie
	{
		protected var id:String;
		private var _isTem:Boolean = false;//是否逐帧绘制阶段
		
		private var bmpMov:IMovie;
		
		private var _src:DisplayObject;
		
		public function BmpSprite() 
		{
			
		}
		
		/**
		 * 当前播放的剪辑的总帧数
		 */
		public function get movTotalFrames():int
		{
			return this.totalFrames;
		}
		
		public function get movCurFrame():int
		{
			return this.bmpMov.currentFrame;
		}
		
		/**
		 * 子类复写
		 * @return
		 */
		protected function getSrc():DisplayObject
		{
			return null;
		}
		
		protected function draw():void
		{
			//TODO:考虑是否延迟刷新
			if (bmpMov)
			{
				this.removeChild(bmpMov as DisplayObject);
				bmpMov = null;
			}
			
			if (SkinPool.getBitmapFrameInfo(id))
			{
				this._isTem = false;
				var frames:Vector.<BitmapFrameInfo> = FrameBmpPool.getBitmapFrameInfo(id);
				bmpMov = new BitmapMovie(frames);
				this.addChild(bmpMov as DisplayObject);
			}
			else
			{
				this._isTem = true;
				this.bmpMov = new TemMov(id, getSrc());
				TemMov(this.bmpMov).addEventListener(TemMov.END_DRAW_FRAME, endDrawFrameHandler);
				this.addChild(this.bmpMov as DisplayObject);
			}
		}
		
		private function endDrawFrameHandler(e:Event):void 
		{
			var temMov:TemMov = e.target as TemMov;
			if(temMov.stage)
			{
				draw();//如果是已经显示的临时序列帧，删除临时序列时，需要再次绘制。
			}
			temMov.removeEventListener(TemMov.END_DRAW_FRAME, endDrawFrameHandler);
		}
		
		/**
		 * 获取当前帧索引
		 */
		public function get currentFrame():int
		{
			return bmpMov.currentFrame;
		}
		
		/**
		 * 获取总的帧数
		 */
		public function get totalFrames():int
		{
			return bmpMov.totalFrames;
		}
		public function stop():void
		{
			this.bmpMov.stop();
		}
		
		public function gotoAndStop(index:int):void
		{
			this.bmpMov.gotoAndStop(index);
		}
		
		public function gotoAndPlay(index:int):void
		{
			this.bmpMov.gotoAndPlay(index);
		}
		
	}

}
